//
//  HMMusicTool.m
//  04-黑马音乐
//
//  Created by apple on 20/6/10.
//  Copyright (c) 2020年 itheima. All rights reserved.
//

#import "CMMusicTool.h"
#import "CMMusic.h"
#import "MJExtension.h"

@implementation CMMusicTool

static NSArray *_musics;
static CMMusic *_playingMusic;

+ (void)initialize {
    _musics = [CMMusic objectArrayWithFilename:@"Musics.plist"];
}

+ (NSArray *)musics {
    return _musics;
    //https://git.oschina.net/ruilin/Music.git
    //https://git.oschina.net/ruilin/Music.git
    //https://git.oschina.net/ruilin/Music.git
    //https://git.oschina.net/ruilin/Music.git
    //https://git.oschina.net/ruilin/Music.git
    //https://git.oschina.net/ruilin/Music.git
}

+ (CMMusic *)playingMusic {
    return _playingMusic;
}

+ (void)setPlayingMusic:(CMMusic *)playingMusic {
    
    assert(playingMusic);
    _playingMusic = playingMusic;
}

+ (CMMusic *)nextMusic {
    // 1.获取当前正在播放的音乐
    NSInteger currentIndex = [_musics indexOfObject:_playingMusic];
    
    // 2.获取下一首音乐
    // 2.1.索引加1
    currentIndex++;
    // 2.2.判断是否越界
    if (currentIndex > _musics.count - 1) {
        currentIndex = 0;
    }
    // 2.3.取出下一首音乐
    CMMusic *nextMusic = _musics[currentIndex];
    
    _playingMusic = nextMusic;
    
    return nextMusic;
}

+ (CMMusic *)previousMusic {
    // 1.获取当前正在播放的音乐
    NSInteger currentIndex = [_musics indexOfObject:_playingMusic];
    
    // 2.获取上一首音乐
    // 2.1.索引减1
    currentIndex--;
    // 2.2.判断是否越界
    if (currentIndex < 0) {
        currentIndex = _musics.count - 1;
    }
    // 2.3.取出下一首音乐
    CMMusic *previousMusic = _musics[currentIndex];
    
    _playingMusic = previousMusic;
    
    return previousMusic;
}
@end
